define(['butterfly/view'], function(View) {
  return View.extend({
    events: {
      'click #login': 'login'
    },

    onShow: function() {
      if(localStorage.getItem('username'))
        window.butterfly.navigate('#main/main.html');
    },

    login: function() {
      var username = this.$('#username').val();
      var password = this.$("#password").val();
      $.ajax({
        url: 'http://115.28.1.119:18860/system/api/system/mobile/accounts/login',
        method: 'POST',
        data: {
          username: username,
          password: password,
          appKey: '9ac10bdf29e6cf120294703c95a60878',
          appId: 'com.foreveross.bsl2'
        }
      }).success(function(data) {
        if(data.result) {
          localStorage.setItem("username", username);
          window.butterfly.navigate('#main/main.html');
        } else {
          alert('帐号或密码错误');
          this.$("#password").val("");
        }
      }).error(function() {

      });
    }

  });
})